<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Auth;

use Illuminate\Support\Facades\DB;

class DashboardController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:web');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //查询总共发布条数
        $games = DB::table('game_data')
            ->where('publisher', Auth::user()->username)
            ->get();

        //查询总共购买横幅条数
        $banner = DB::table('banner_record')
            ->where('buyer', Auth::user()->username)
            ->get();

        //查询总共推广游戏条数
        $promotion = DB::table('promotion')
            ->where('promoter', Auth::user()->username)
            ->get();

        //查询总共收益
        $income = DB::table('income_record')
            ->where('income_person', Auth::user()->username)
            ->sum('single');

        //查询总共充值
        $pay = DB::table('pay_record')
            ->where('username', Auth::user()->username)
            ->where('trade_status',1)
            ->sum('total_amount');

        //查询发布游戏支出
        $game_outlay = DB::table('game_data')
            ->where('publisher', Auth::user()->username)
            ->sum('consume');

        //查询购买横幅支出
        $banner_outlay = DB::table('banner_record')
            ->where('consume', Auth::user()->username)
            ->sum('consume');

        //查询推广游戏支出
        $promotion_outlay = DB::table('promotion_record')
            ->where('promoter', Auth::user()->username)
            ->sum('single');

        //当前日期
        $sdefaultDate = date('Y-m-d');
        //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
        $first=1;
        //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
        $w=date('w',strtotime($sdefaultDate));
        //获取本周开始日期，如果$w是0，则表示周日，减去 6 天
        $week_start=date('Y-m-d',strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days'));
        //本周结束日期
        $week_end=date('Y-m-d',strtotime("$week_start +6 days"));


        //查询本周收益
        $week_income = DB::table('income_record')
            ->whereDate('time','>=', $week_start)
            ->whereDate('time','<=', $week_end)
            ->where('income_person', Auth::user()->username)
            ->sum('single');

        //查询本周充值
        $week_pay = DB::table('pay_record')
            ->whereDate('created_at','>=', $week_start)
            ->whereDate('created_at','<=', $week_end)
            ->where('username', Auth::user()->username)
            ->where('trade_status',1)
            ->sum('total_amount');

        return view('user.dashboard.index',[
            'games'=>$games->count(),
            'banner'=>$banner->count(),
            'promotion'=>$promotion->count(),
            'outlay'=> $game_outlay+$banner_outlay+$promotion_outlay,
            'income'=>$income,
            'pay'=>$pay,
            'week_income' => $week_income,
            'week_pay' => $week_pay,
            'week_sum' => $week_income+$week_pay
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
